<template>
  <div class="app-container">
    <el-card header="查询条件" shadow="never">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input v-model="listQuery.username" placeholder="请输入用户名称" prefix-icon="el-icon-office-building" />
        </el-col>
        <el-col :span="4">
          <el-select v-model="listQuery.userType" placeholder="建筑属性" @focus="focus">
            <el-option label="写字楼" value="1" />
            <el-option label="住宅" value="2" />
            <el-option label="酒店" value="2" />
            <el-option label="商业" value="2" />
          </el-select>
        </el-col>
        <el-col :span="12">
          <el-button type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="handleCreate">添加</el-button>
          <!-- <el-button type="primary" icon="el-icon-delete-solid">批量删除</el-button> -->
          <el-button type="primary" icon="el-icon-s-promotion">导出</el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- 表格 -->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableList"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column prop="id" label="ID" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="建筑名称" width="200">
        <template slot-scope="{row}">
          <span>{{ row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="userType" label="建筑属性" width="100">
        <template slot-scope="{row}">
          <span>{{ row.userType }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="totalArea" label="总面积(㎡)">
        <template slot-scope="{row}">
          <span>{{ row.totalArea }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="supplyArea" label="供能面积(㎡)">
        <template slot-scope="{row}">
          <span>{{ row.supplyArea }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="supplyRate" label="供能比例">
        <template slot-scope="{row}">
          <span>{{ row.supplyRate }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="ecoRate" label="节能率">
        <template slot-scope="{row}">
          <span>{{ row.ecoRate }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="heatLoad" label="设计热负荷（KW）">
        <template slot-scope="{row}">
          <span>{{ row.heatLoad }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="coldLoad" label="设计冷负荷（KW）">
        <template slot-scope="{row}">
          <span>{{ row.coldLoad }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px">
        <template slot-scope="{row,$index}">
          <el-button mc-type="column-el-button" size="mini" type="primary" @click="handleUpdate(row)">编辑</el-button>
          <el-button mc-type="column-el-button" size="mini" type="danger" @click="handleDelete(row,$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 弹框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="建筑名称" prop="username" required>
          <el-input v-model="temp.username" />
        </el-form-item>
        <el-form-item label="建筑属性" prop="userType" required>
          <el-select v-model="temp.userType" class="filter-item" placeholder="Please select">
            <!-- <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" /> -->
            <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="总面积" prop="totalArea" required>
          <el-input v-model="temp.totalArea" />
        </el-form-item>
        <el-form-item label="供能面积" prop="supplyArea" required>
          <el-input v-model="temp.supplyArea" />
        </el-form-item>
        <el-form-item label="供能比例" prop="supplyRate" required>
          <el-input v-model="temp.supplyRate" />
        </el-form-item>
        <el-form-item label="节能率" prop="ecoRate" required>
          <el-input v-model="temp.ecoRate" />
        </el-form-item>
        <el-form-item label="设计热负荷（KW）" prop="heatLoad" required>
          <el-input v-model="temp.heatLoad" />
        </el-form-item>
        <el-form-item label="设计冷负荷（KW）" prop="coldLoad" required>
          <el-input v-model="temp.coldLoad" />
        </el-form-item>
        <!-- 可参考，时间 -->
        <!-- <el-form-item label="Date" prop="timestamp">
          <el-date-picker v-model="temp.timestamp" type="datetime" placeholder="Please pick a date" />
        </el-form-item>

        <!-- 可参考，下拉框 -->
        <!--
        <el-form-item label="Status">
          <el-select v-model="temp.status" class="filter-item" placeholder="Please select">
            <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item" />
          </el-select>
          -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          Cancel
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          Confirm
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { systemUserList } from '@/api/cfg'

export default {
  name: 'UserConfig',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        username: undefined,
        userType: undefined,
        sort: '+id'
      },
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      dialogFormVisible: false, // 弹框是否显示
      dialogStatus: '', // 弹框类型：修改/新增
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      tableList: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取数据
    getList() {
      this.listLoading = true
      systemUserList(this.listQuery).then(response => {
        this.tableList = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.dialogFormVisible = false
      this.$notify({
        title: 'Success',
        message: 'Created Successfully',
        type: 'success',
        duration: 2000
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.dialogFormVisible = false
      this.$notify({
        title: 'Success',
        message: 'Update Successfully',
        type: 'success',
        duration: 2000
      })
    },
    handleDelete(row, index) {
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      this.list.splice(index, 1)
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
